package com.we.scrm.web;

import com.we.scrm.common.enums.TagsTypeEnum;
import com.we.scrm.common.page.Pagination;
import com.we.scrm.common.util.JsonView;
import com.we.scrm.domain.Tags;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TagsController extends AbstractController {
    @RequestMapping("/tagsPage")
    public ModelAndView tagsPage(){
        return prepareModelAndView("tagsPage");
    }
    //获取标签列表 - 分页
    @PostMapping("/tags/list")
    @ResponseBody
    public String tagsList(Tags queryEntity, Pagination<Tags> page){
        page = tagsService.queryPage(queryEntity, page);
        return JsonView.successJson(page);
    }
    //获取标签类型列表
    @RequestMapping("tagsType/list")
    @ResponseBody
    public String tagsTypeList(){
        return JsonView.successJson(TagsTypeEnum.toList());
    }
    // 更新标签
    @RequestMapping("/tags/update")
    @ResponseBody
    public String tagsUpdate(Tags entity){
        if(StringUtils.isNotBlank(entity.getContent())){
            if(null != entity.getId()){//是否是添加
                tagsService.update(entity);
            }else{
                tagsService.create(entity);
            }
        }else{
            return JsonView.failureJson("标签内容不能为空");
        }
        return JsonView.successJson();
    }
    @RequestMapping("tags/delete")
    @ResponseBody
    public String tagsDelete(Tags entity){
        tagsService.delete(entity);
        return JsonView.successJson();
    }
    @RequestMapping("/tags/all")
    @ResponseBody
    public String getAll(Tags queryEntity) {
        return JsonView.successJson(tagsService.queryAll(queryEntity));
    }
}